import datetime
import os.path

from django.conf import settings


def upload_to(instance, filename):
    """
    :param instance: 上传文件的模型
    :param filename: 文件名
    :return: 图片路径
    """
    model_name = instance._meta.model_name  # 获取模型名
    date_str = str(datetime.date.today())
    date_log = datetime.datetime.now().strftime("%Y-%m-%d:%H%M%S")
    new_filename = f"{date_log}-{filename}"
    image_path = os.path.join(
        os.path.join(os.path.join(os.path.join(settings.MEDIA_ROOT, model_name), date_str), new_filename))

    return 'media' + image_path
